package com.liehacker.android.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import android.content.Context;

public class Config {
	private Map<String, String> map;
	private static Context context;
	private static Config singleton = null;
	public static final String PATH = "mapData";	
	public static final String APP_URL = "APP_URL";

	//liehacker 허니컴 
	private static Set<String> honeycombDevices = new HashSet<String>();
	static {
		getHoneycombDevices().add("MZ601");		
		getHoneycombDevices().add("umts_hubble");
		getHoneycombDevices().add("SHW-M380K");
	}

	public static void setContext(Context context) {
		Config.context = context;
	}
	
	public static Config getInstance() {
		
		if( singleton == null )
			singleton = new Config();
		
		return singleton;
	}
	
	private Config() { 
		load();
	}
	
	private void save() {
		if(context == null) {
			return;
		}
		try {
			FileOutputStream fileSave = context.openFileOutput(PATH, 0);
			ObjectOutputStream objSave = new ObjectOutputStream(fileSave);
			objSave.writeObject(map);
			objSave.close();
		} catch(IOException e) {
			e.printStackTrace();
		}

	}

	private void setDefaults() {		
		setDefault(APP_URL, "tving");			
	}
	
	private void setDefault(String k, String v) {
		if(map.containsKey(k)) {
			return;
		}
		map.put(k, v);
	}
	
	private void load() {
		try{
			File file = context.getFileStreamPath(PATH);
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			FileInputStream fileLoad = context.openFileInput(PATH);
			ObjectInputStream objLoad = null;
			objLoad = new ObjectInputStream(new BufferedInputStream(fileLoad));
			map = (Map<String, String>)objLoad.readObject();
		} catch(Throwable t) {
			map = new HashMap<String, String>();
			save();
//			t.printStackTrace();
		}
		setDefaults();
	}

	public static void setHoneycombDevices(Set<String> honeycombDevices) {
		Config.honeycombDevices = honeycombDevices;
	}

	public static Set<String> getHoneycombDevices() {
		return honeycombDevices;
	}
	
}
